<template>
    <div>
        <div v-drag id="drag" class="drag">
            <div class="title">当前剩余百分比 <em>(%)</em></div>
            <count-to class="percent el-info" :class="{'el-danger': percent > 100, 'el-success': percent === 100 }" :startVal="0" :endVal="percent" :duration="1000"></count-to>
        </div>
    </div>
</template>
<script>
import CountTo from 'vue-count-to'
import { mapGetters } from 'vuex'

export default {
    name: 'percent',
    data(){
        return {
            orderTotal: 0
        }
    },
    computed:{
        ...mapGetters([
            'percent'
        ])
    },
    components: {
        CountTo
    },
    directives: {
        'drag': {
            bind(el, bindings) {
                el.onmousedown = (e) => {
                    //获取鼠标点击处分别与div左边和上边的距离：鼠标位置-div位置
                    var divx = e.clientX - document.getElementById('drag').offsetLeft;
                    var divy = e.clientY - document.getElementById('drag').offsetTop;
                    //包含在onmousedown里，表示点击后才移动，为防止鼠标移出div，使用document.onmousemove
                    document.onmousemove = function(e) {
                        //获取移动后div的位置：鼠标位置-divx/divy
                        var l = e.clientX - divx;
                        var t = e.clientY - divy;
                        document.getElementById('drag').style.left = l + 'px';
                        document.getElementById('drag').style.top = t + 'px';
                    }
                    document.onmouseup = function(e) {
                        document.onmousemove = null;
                        document.onmouseup = null;
                    }
                }
            }
        }
    }
}
</script>
<style scoped>
.drag {
    cursor: move;
    width: 200px;
    height: 100px;
    position: fixed;
    top: 5%;
    left: 75%;
    z-index: 10;
    border: 1px solid #B7B6B4;
    border-radius: 5px;
    box-shadow: 1px 1px 2px #B7B6B4;
    background-color: #fff;
    padding: 10px;
}
.title{
    color: #707072;
    font-size: 14px;
}
.percent {
    font-size: 30px;
    font-weight: bold;
    color: #707072;
    margin-left: 50%;
    line-height:58px;
}

</style>
